package cn.hh.pet.service.impl;

import cn.hh.basic.exception.BusinessException;
import cn.hh.basic.service.IBaiduAiAuditService;
import cn.hh.basic.util.LoginContext;
import cn.hh.org.domain.Employee;
import cn.hh.org.mapper.EmployeeMapper;
import cn.hh.pet.domain.Pet;
import cn.hh.pet.domain.PetDetail;
import cn.hh.pet.domain.PetOnlineAuditLog;
import cn.hh.pet.mapper.PetDetailMapper;
import cn.hh.pet.mapper.PetMapper;
import cn.hh.pet.mapper.PetOnlineAuditLogMapper;
import cn.hh.pet.service.IPetService;
import cn.hh.basic.service.impl.BaseServiceImpl;
import cn.hh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 添加宠物明细
     *
     * @param pet
     */
    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);

        // 获取宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            // 如果不为空就设置宠物id
            petDetail.setPetId(pet.getId());
            // 在添加petDetail宠物明细
            petDetailMapper.save(petDetail);
        }

    }

    @Override
    public void update(Pet pet) {
        super.update(pet);

        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            // 如果不为空就设置宠物id
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }

    }

    /**
     * 根据宠物id删除宠物明细
     *
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);

        petDetailMapper.delPetDetailByPetId(id);

    }

    /**
     * 宠物上架
     *
     * @param ids
     * @param request
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        // 获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            // 先拿到每一个pet
            Pet pet = petMapper.loadById(id);
            // 1.上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imagedBoo = baiduAiAuditService.imageAudit(pet.getResources());
            if (textBoo && imagedBoo) {
                // 2.审核通过: 修改状态 + 添加上架的审核成功日志
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                // 添加审核日志
                // 创建一个PetOnlineAuditLog对象
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                // 使用PetOnlineAuditLog对象设置
                log.setPetId(pet.getId());  // 获取petId,设置审核日志中去
                log.setState(1); // 1审核通过  0待审核  -1驳回   审核状态
                log.setAuditTime(new Date()); // 设置审核日志上架时间
                log.setNote("审核成功"); // 设置审核日志
                log.setAuditId(employee.getId()); // 设置审核人
                // 最后添加日志
                petOnlineAuditLogMapper.save(log);

            } else {
                // 3.审核失败: 添加上架的审核的失败日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());  // 获取petId,设置审核日志中去
                log.setState(-1); // 1审核通过  0待审核  -1驳回   审核状态
                log.setAuditTime(new Date()); // 设置审核日志上架时间
                log.setNote("审核失败,内容有敏感信息!"); // 设置审核日志
                log.setAuditId(employee.getId()); // 设置审核人
                // 最后添加日志
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败,内容有敏感信息!");

            }
        }


    }

    /**
     * 宠物下架
     *
     * @param ids
     */
    @Override

    public void offsale(List<Long> ids) {
        // 下架时间
        Date offsaletime = new Date();
        petMapper.offsale(offsaletime, ids);
    }
}
